
import com.jason.entity.Category;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.primefaces.component.menubar.Menubar;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

/**
 *
 * @author Jason
 */
@ManagedBean
@ApplicationScoped
public class BaseConstruct {

    private Category category;
    private List<Category> categorys;
    private TomcatBean dao;
    private TreeNode tree;
    private Menubar menubar;
    @ManagedProperty("#{appDataSources}")
    private AppDataSources app;

    @PostConstruct
    public void init() {
        dao = new TomcatBean(app.getEm());
    }

    public Menubar getMenubar() {
        menubar = getMyMenubar(dao.getRootNode());
        return menubar;
    }

    private Menubar getMyMenubar(List<Category> categorys) {
        Menubar menubara = new Menubar();
        menubar = (Menubar) getMenubar(menubara, categorys);
        return menubara;
    }

    private UIComponent getMenubar(UIComponent ui, List<Category> categorys) {
        for (Category c : categorys) {
            if (c.getCategoryList().isEmpty()) {
                MenuItem menuItem = new MenuItem();
                menuItem.setAjax(c.getAjax());
                menuItem.setAsync(c.getAsync());
                menuItem.setDisabled(c.getDisabled());
                menuItem.setIncludeViewParams(c.getIncludeViewParams());
                menuItem.setPartialSubmit(c.getPartialSubmit());
                menuItem.setGlobal(c.getGlobal());

                menuItem.setUrl(c.getUrl());
                menuItem.setFragment(c.getFragment());
                menuItem.setIcon(c.getIcon());
                menuItem.setOnclick(c.getOnclick());
                menuItem.setOncomplete(c.getOncomplete());
                menuItem.setOnerror(c.getOnerror());
                menuItem.setOnstart(c.getOnstart());
                menuItem.setOnsuccess(c.getOnsuccess());
                menuItem.setProcess(c.getProcess());
                menuItem.setStyle(c.getStyle());
                menuItem.setStyleClass(c.getStyleClass());
                menuItem.setTarget(c.getTarget());
                menuItem.setTitle(c.getTitle());
                menuItem.setUpdate(c.getUpdateRender());
                menuItem.setValue(c.getName());

                ui.getChildren().add(menuItem);
            } else {
                Submenu submenu = new Submenu();
                submenu.setLabel(c.getName());
                submenu.setIcon(c.getIcon());
                submenu.setStyle(c.getStyle());
                submenu.setStyleClass(c.getStyleClass());
                ui.getChildren().add(submenu);
                getMenubar(submenu, c.getCategoryList());
            }
        }
        return ui;
    }

    private TreeNode getTeeNode(TreeNode tree, List<Category> cs) {
        for (Category c : cs) {
            DefaultTreeNode node = new DefaultTreeNode(c, tree);
            if (!c.getCategoryList().isEmpty()) {
                getTeeNode(node, c.getCategoryList());
            }
        }


        return tree;
    }

    public void setMenubar(Menubar menubar) {
        this.menubar = menubar;
    }

    public List<Category> getAll() {
        return dao.getAll();
    }

    public Category getCategory() {
        if (category == null) {
            category = new Category();
        }
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public TreeNode getTree() {
        tree = new DefaultTreeNode("ROOT", null);
        for (Category c : dao.getRootNode()) {

            TreeNode node = new DefaultTreeNode(c, tree);
            getTeeNode(node, c.getCategoryList());

        }
        return tree;
    }

    public CategoryConverter getConverter() {
        return new CategoryConverter();
    }

    public List<Category> getCategorys() {
        categorys = dao.getRootNode();
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    class CategoryConverter implements Converter {

        @Override
        public Category getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || "".equals(value) || "first".equals(value)) {
                return null;
            }
            return dao.findById(Integer.valueOf(value));
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            if (value == null || "".equals(value)) {
                return null;
            }

            return String.valueOf(((Category) value).getId());
        }
    }

    public AppDataSources getApp() {
        return app;
    }

    public void setApp(AppDataSources app) {
        this.app = app;
    }
}
